//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shp.Runtime.Common;
using Shp.Runtime.Sitka.ServiceAgent.Managers;
using Shp.Runtime.Sitka.ServiceAgent;
using Shp.Runtime.Sitka.ServiceAgent.Entities;
using Shp.Runtime.Sitka.ServiceAgent.EntityMappers;
using System.Configuration;

namespace LitwareHR.Portfolio.DataModel.Sitka.Tests.Mocks
{
	public class RepositoryFactory : IRepositoryFactory
	{		
		private Guid tenantId;
		private static string LitwareContainerKey = ConfigurationManager.AppSettings["LitwareContainerKey"];

		public RepositoryFactory(Guid tenantId)
		{			
			this.tenantId = tenantId;
			CreateMetadataContainer();
		}
		

		public void CreateMetadataContainer()
		{
			if (!ContainerManager.ContainerExistsForTenant(LitwareContainerKey))
			{
				ContainerManager.CreateContainer(LitwareContainerKey);
			}
		}

		public void CreateTenantContainer()
		{
			if (!ContainerManager.ContainerExistsForTenant(tenantId.ToString()))
			{
				ContainerManager.CreateContainer(tenantId.ToString());
			}
		}

		public void DeleteMetadataContainer()
		{
			if (ContainerManager.ContainerExistsForTenant(LitwareContainerKey))
			{
				ContainerManager.DeleteContainer(LitwareContainerKey);
			}
		}

		public void DeleteTenantContainer()
		{
			if (ContainerManager.ContainerExistsForTenant(tenantId.ToString()))
			{
				ContainerManager.DeleteContainer(tenantId.ToString());
			}
		}

		public Repository<GenericExtensibleEntity> MetadataRepository
		{
			get
			{				
				return new Repository<GenericExtensibleEntity>(LitwareContainerKey, new GenericExtensibleEntityMapper());

			}
		}

		public Repository<GenericExtensibleEntity> GetDataRepository(Guid tenantId)
		{
			return new Repository<GenericExtensibleEntity>(tenantId.ToString(), new GenericExtensibleEntityMapper());
		}
	}
}
